/**
 * 添加或者修改页面
 */
var AreaInfoDlg = {
    data: {
        id: "",
        name: "",
        fullName: "",
        pinYin: "",
        location: "",
        cIdx: "",
        parentId: "",
        createUser: "",
        createTime: "",
        updateUser: "",
        updateTime: "",
        deleteFlag: ""
    }
};

layui.use(['form', 'admin', 'ax', 'laydate', 'upload', 'tree','layer'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;

    let tree = layui.tree

    let Area = {
        tableId: "areaTableAdd"
    };
    //加载区域数据
    function loadListTree() {
        var ajax = new $ax(Changein.ctxPath + "/area/listTreeJump", function (data) {
            loadTreeList(data.data);
        }, function (data) {
        });
        ajax.start();
    }
    loadListTree();

    //开启节点操作图标
    Area.treeList = function (dataList) {

    }

    //加载区域树
    function loadTreeList(dataList) {
        tree.render({
            elem: '#' + Area.tableId
            , data: dataList
            , edit: ['add', 'update', 'del'] //操作节点的图标
            , showCheckbox: true  //是否显示复选框
            , onlyIconControl: true
            , accordion: true //是否开启手风琴模式，默认 false
            , showLine: false
            , id: Area.tableId //定义索引
            ,text: {
                defaultNodeName: '节点名称' //节点默认名称
                ,none: '无数据' //数据为空时的提示文本
            }
            , click: function (obj) {
                // layer.msg(JSON.stringify(obj.data));
            }, operate: function (obj) { //得到操作类型：add、edit、del
                var type = obj.type; //得到操作类型：add、edit、del
                var data = obj.data; //得到当前节点的数据
                var elem = obj.elem; //得到当前节点元素

                //Ajax 操作
                var id = data.id; //得到节点索引
                let title = data.title;
                let msg = "";
                let url = "";
                let ofType = 0;
                let dataParam = {fullName: title, name: title};
                if (type === 'add') { //增加节点
                    //返回 key 值
                    url = "/area/addItem";
                    msg = "新增";
                    dataParam.parentId = id;
                    ofType = 1;
                    layer.prompt({
                        formType: 2,
                        value: title + "子节点",
                        title: '添加区域名称',
                    }, function (value, index, elem) {
                        dataParam.fullName = value;
                        dataParam.name = value;
                        var ajax = new $ax(Changein.ctxPath + url, function (data) {
                            Changein.success(msg + "成功！");
                            layer.close(index);
                            loadListTree(); //重新加载数据
                        });
                        ajax.set(dataParam);
                        ajax.start();
                    });
                } else if (type === 'update') { //修改节点
                    url = "/area/editItem";
                    msg = "修改";
                    dataParam.id = id;
                } else if (type === 'del') { //删除节点
                    url = "/area/editItem";
                    msg = "删除";
                    dataParam.deleteFlag = 1;
                    dataParam.id = id;
                }

                if (ofType != 1) {
                    var ajax = new $ax(Changein.ctxPath + url, function (data) {
                        Changein.success(msg + "成功！");
                    }, function (data) {
                    });
                    ajax.set(dataParam);
                    ajax.start();
                }

            }
        });
    }

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        //获得选中的节点
        var checkData = tree.getChecked(Area.tableId);
        console.log("checkData==" + checkData);
        /*var ajax = new $ax(Changein.ctxPath + "/area/addItem", function (data) {
            Changein.success("添加成功！");
            window.location.href = Changein.ctxPath + '/area'
        }, function (data) {
            Changein.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();*/

        return false;
    });

    $('#cancel').click(function () {
        window.location.href = Changein.ctxPath + '/area'
    });

});